package mysql

import (
	"gorm.io/gorm"
	user "videozg3/common/proto"
)

type Video struct {
	gorm.Model
	Title        string `gorm:"type:varchar(20)" json:"title"`
	Desc         string `gorm:"type:varchar(100)" json:"desc"`
	UserId       int64  `gorm:"type:int" json:"userId"`
	MusicId      int64  `gorm:"type:int" json:"musicId"`
	TagId        int64  `gorm:"type:int" json:"tagId"`
	CommentCount int64  `gorm:"type:int" json:"commentCount"`
	LikesCount   int64  `gorm:"type:int" json:"likesCount"`
	VideoType    int64  `gorm:"type:int" json:"videoType"`
}

func (v *Video) FindVideos(offset, limit int64) (video []*Video, err error) {
	err = DB.Model(&v).Offset(int(offset)).Limit(int(limit)).Find(&video).Error
	if err != nil {
		return nil, err
	}
	return video, nil
}
func (v *Video) AddVideo(in *user.AddVideoReq) error {
	vide := &Video{
		Title:     in.Title,
		Desc:      in.Desc,
		UserId:    in.Userid,
		MusicId:   in.MusicId,
		TagId:     in.TagId,
		VideoType: in.VideoType,
	}
	err := DB.Create(&vide).Error
	if err != nil {
		return err
	}
	return nil
}
func (v *Video) FindVideo(id int64) (*Video, error) {
	err := DB.Model(&v).Where("id=?", id).Find(&v).Error
	if err != nil {
		return nil, err
	}
	return v, nil
}
func (v *Video) UpdateLikeCount(id int64, count int) error {
	return DB.Model(&v).Where("id=?", id).Update("likes_count", count).Error
}
